package com.smartstate.EnvironmentMonitor.websocket;

import com.alibaba.fastjson.JSONArray;
import com.smartstate.EnvironmentMonitor.PowerEquipmentJobClient;
import com.smartstate.EnvironmentMonitor.dto.EquipmentDTO;
import com.smartstate.EnvironmentMonitor.service.AlarmService;
import com.smartstate.EnvironmentMonitor.vo.AlarmVo;
import com.smartstate.EnvironmentMonitor.vo.EquipMentVo;
import com.smartstate.EnvironmentMonitor.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

@Component("ddd")
public class WebSocketServerDateTime {


    @Autowired
    private AlarmService alarmService;

    @Autowired
    PowerEquipmentJobClient powerEquipmentJobClient;

    @Autowired
    RedisTemplate redisTemplate;


    public static Map<String, String> ddd = new HashMap<>();


    @Scheduled(cron = "0/1 * * * * ?")
    public void as() {
        List<Map> listMap = new ArrayList<>();
        for (Map.Entry<String, String> item : ddd.entrySet()) {
            if (item.getValue() != null) {
                //Redis中设备的属性和值
                List<String> list = Arrays.asList(item.getValue().split(","));

                for (String deviceId : list) {
                    Map entries = redisTemplate.opsForHash().entries(deviceId);
                    EquipMentVo equipMentVo = alarmService.selectTags(deviceId);
                    if (equipMentVo != null && equipMentVo.getTags() != null && !"".equals(equipMentVo.getTags())) {
                        List<EquipmentDTO.AddressData> addressDataList = JSONArray.parseArray(equipMentVo.getTags(), EquipmentDTO.AddressData.class);


                        if (entries.size() > 0) {
                            addressDataList.stream().forEach(addressData -> {

                                if (entries.containsKey(addressData.getAttributeName()) && addressData.getDeviation() != null && !"".equals(addressData.getDeviation())) {
                                    float deviation = Float.parseFloat(addressData.getDeviation());
                                    BigDecimal deviationDecimal = new BigDecimal(deviation);
                                    BigDecimal nowBigDecimal = new BigDecimal(Float.parseFloat(entries.get(addressData.getAttributeName()).toString()));
                                    float v = nowBigDecimal.add(deviationDecimal).floatValue();
                                    entries.put(addressData.getAttributeName(), v);
                                }

                                if (entries.containsKey(addressData.getAttributeName()) && ("longParticle".equals(addressData.getAttributeName()) || "littleParticle".equals(addressData.getAttributeName()))) {

                                    //保留两位
                                    float parseFloat = Float.parseFloat(entries.get(addressData.getAttributeName()).toString());
                                    //保留小数
                                    BigDecimal big = new BigDecimal(parseFloat);
                                    BigDecimal a = big.setScale(2,BigDecimal.ROUND_DOWN);

                                    entries.put(addressData.getAttributeName(), a.toString());
                                } else {
                                    //保留一位小数
                                    float parseFloat = Float.parseFloat(entries.get(addressData.getAttributeName()).toString());
                                    //保留小数
                                    BigDecimal big = new BigDecimal(parseFloat);
                                    BigDecimal a = big.setScale(1,BigDecimal.ROUND_DOWN);
                                    entries.put(addressData.getAttributeName(), a.toString());
                                }
                            });
                        }
                    }

                    Map mapItem = new HashMap();
                    mapItem.put(deviceId, entries);
                    listMap.add(mapItem);
                }
                WebSocket.send(ResultVO.ok(listMap, "实时数据面板"), item.getKey());
            }
        }
        List<AlarmVo> list = new ArrayList<>();
        if (alarmService.selectAlarmTime().size() > 0) {
            list = alarmService.selectAlarmTime();
            WebSocket.sendInfo(ResultVO.ok(list, "报警信息列表"));
        } else {
            WebSocket.sendInfo(ResultVO.ok(list, "报警信息列表"));
        }

    }
}
